In trying to create a simple PHP PDO update function that if the field is not found would insert it, I created this little snippet.
function updateorcreate($table,$name,$value){
global $sodb;
$pro = $sodb->prepare("UPDATE `$table` SET value = :value WHERE field = :name");
if(!$pro){
$pro = $sodb->prepare("INSERT INTO `$table` (field,value) VALUES (:name,:value)");
}
$pro->execute(array(':name'=>$name,':value'=>$value));
}
It does not detect though if the update function is going to work with if(!$pro);
How would we make this one work.
global
– PeeHaa Sep 5 '12 at 14:29execute()
it before you can tell whether the update caused rows to get updated usingnumRows()
, but if the field already exists with the same value you might get unintended results as well. – Ja͢ck Sep 5 '12 at 14:30